﻿Public Class Perfiles

    Private oAdmin As New CAdmin
    Private idPerfil As Integer

    Private Sub Perfiles_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.CargarPerfiles()
    End Sub

    Private Sub CargarPerfiles()
        Try
            Dim ds As New DataSet
            ds = Me.oAdmin.GetPerfiles()
            If (ds.Tables(0).Rows.Count > 0) Then
                Me.grdPerfil.DataSource = Nothing
                Me.grdPerfil.Columns.Clear()
                Me.grdPerfil.DataSource = ds.Tables(0)

                Dim iconoEditar As New DataGridViewImageColumn()
                iconoEditar.ValuesAreIcons = False
                iconoEditar.Image = My.Resources.editar
                iconoEditar.HeaderText = "EDITAR"
                iconoEditar.Name = "Editar"
                Me.grdPerfil.Columns.Add(iconoEditar)

                Dim iconoEliminar As New DataGridViewImageColumn()
                iconoEliminar.ValuesAreIcons = False
                iconoEliminar.Image = My.Resources.eliminar
                iconoEliminar.HeaderText = "ELIMINAR"
                iconoEliminar.Name = "Eliminar"
                Me.grdPerfil.Columns.Add(iconoEliminar)

                Me.grdPerfil.Columns("ID").Visible = False
                Me.grdPerfil.Columns("AEREO").Visible = False
                Me.grdPerfil.Columns("MARITIMO").Visible = False
                Me.grdPerfil.Columns("EDITAR").Width = 50
                Me.grdPerfil.Columns("ELIMINAR").Width = 70
            Else
                Me.grdPerfil.DataSource = Nothing
                Me.grdPerfil.Columns.Clear()
                MessageBox.Show("No existen perfiles", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MessageBox.Show("Ocurrió un error al cargar los perfiles: " & ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub grdPerfil_CellMouseEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdPerfil.CellMouseEnter
        '*** Coloca la manito cuando el mouse entra en las celdas de editar y eliminar
        If (Me.grdPerfil.Columns(e.ColumnIndex).Name = "Editar" Or Me.grdPerfil.Columns(e.ColumnIndex).Name = "Eliminar") Then
            Cursor = Cursors.Hand
        Else
            Cursor = Cursors.Default
        End If
    End Sub

    Private Sub grdPerfil_CellMouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles grdPerfil.CellMouseMove
        If (Me.grdPerfil.Columns(e.ColumnIndex).Name = "Editar" Or Me.grdPerfil.Columns(e.ColumnIndex).Name = "Eliminar") Then
            Cursor = Cursors.Hand
        Else
            Cursor = Cursors.Default
        End If
    End Sub

    Private Sub grdPerfil_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdPerfil.CellDoubleClick
        '*** Permite editar o eliminar
        'Se obtiene la info del registro
        Dim id As Integer = Me.grdPerfil.Rows(e.RowIndex).Cells("ID").Value
        Dim perfil As String = Me.grdPerfil.Rows(e.RowIndex).Cells("PERFIL").Value

        Select Case Me.grdPerfil.Columns(e.ColumnIndex).Name
            Case "Editar"
                Me.EditarPerfil(id, perfil)
            Case "Eliminar"
                Me.EliminarPerfil(id)
        End Select
    End Sub

    Private Sub EditarPerfil(ByVal id As Integer, ByVal perfil As String)
        '"Sube" los datos del registro seleccionado
        Me.idPerfil = id
        Me.txtPerfil.Text = perfil        
    End Sub

    Private Sub EliminarPerfil(ByVal id As Integer)
        Try
            If (MessageBox.Show("¿Realmente desea eliminar este perfil?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
                Cursor = Cursors.WaitCursor
                Me.oAdmin.EliminarPerfil(id)
                MessageBox.Show("El perfil ha sido eliminado", "", MessageBoxButtons.OK, MessageBoxIcon.Information)

                Cursor = Cursors.Default
            End If
        Catch ex As Exception
            Cursor = Cursors.Default
            MessageBox.Show("NO se eliminó el perfil: " & ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try

        Me.CargarPerfiles()
    End Sub

    Private Sub btnListo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListo.Click
        'Crea/Edita el perfil
        Try            
            If (Trim(Me.txtPerfil.Text) = "") Then
                MessageBox.Show("Debe escribir el nombre del perfil que desea crear o modificar", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                Cursor = Cursors.WaitCursor
                Dim result As String = Me.oAdmin.ActualizarPerfil(Me.idPerfil, Trim(Me.txtPerfil.Text))
                Cursor = Cursors.Default
                MessageBox.Show(result, "", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            Cursor = Cursors.Default
            MessageBox.Show("Ocurrió un error al crear/modificar el perfil" & ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try

        Me.Limpiar()
        Me.CargarPerfiles()
    End Sub

    Private Sub Limpiar()
        Me.idPerfil = 0
        Me.txtPerfil.Text = ""
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Limpiar()
    End Sub
End Class